package com.team10.hotel.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.team10.hotel.domain.Hotel;
import com.team10.hotel.domain.Room;

import com.team10.hotel.domain.RoomImage;
import com.team10.hotel.service.HotelService;
import com.team10.hotel.service.RoomImageService;
import com.team10.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private HotelService hotelService;
    @Autowired
    private RoomImageService roomImageService;
    @GetMapping("/list")
    public String listRoomPage(@RequestParam("id")int id, Model model){
        List<Room> rooms = roomService.listRoom(id);
        Hotel hotel = hotelService.findHotelByPrimaryKey(id);
        model.addAttribute("rooms",rooms);
        model.addAttribute("hotel",hotel);
        return "roomList";
    }
    @GetMapping("/add")
    public String addRoom(@RequestParam("id")int id, Model model){
        Hotel hotel = hotelService.findHotelByPrimaryKey(id);
        model.addAttribute("hotel",hotel);
        return "roomAdd";
    }
    @PostMapping("/add")
    public String saveRoom(Room room, @RequestParam("file")MultipartFile []file) throws FileNotFoundException {
        String path= ResourceUtils.getURL("").getPath()+"src/main/resources/static/img/";
        path=path.substring(1);
        int insert=roomService.insertRoom(room);
        int hotelId=room.getHotelId();
        int roomId=roomService.selectLastInsertId();
        System.out.println(path);
        int len=file.length;
        for (int i=0;i<len;i++){
            String fileName=file[i].getOriginalFilename();
            String suffixName=fileName.substring(fileName.lastIndexOf("."));
            fileName= UUID.randomUUID()+suffixName;
            File dest=new File(path+fileName);
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }    try {
                file[i].transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            RoomImage roomImage=new RoomImage();
            roomImage.setRoomId(roomId);
            roomImage.setImageName(fileName);
            roomImageService.saveRoomImage(roomImage);
        }
        
        if(insert>0) {
            return "redirect:/room/list?id="+hotelId;
        } else {
            return "redirect: error";
        }
    }
    @GetMapping("/delete")
    public  String deleteRoom(@RequestParam("id")int id) throws FileNotFoundException {
        String path= ResourceUtils.getURL("").getPath()+"src/main/resources/static/img/";
        List<RoomImage>roomImages=roomImageService.listRoomImage(id);
        for (RoomImage roomImage:roomImages
             ) {
            File file=new File(path+roomImage.getImageName());
            if (file.exists()) file.delete();
        }
        Room room=roomService.findRoomByPrimaryKey(id);
        int hotelId=room.getHotelId();
        int deleted=roomService.deleteRoomByPrimaryKey(id);
        if(deleted>0) {
            return "redirect:/room/list?id="+hotelId;
        } else {
            return "redirect: error";
        }
    }
    @RequestMapping("/deleteImage")
    @ResponseBody
    public  Boolean deleteImage(int[] imageId) throws FileNotFoundException {
        String path= ResourceUtils.getURL("src/main/resources/static/img/").getPath();
        for (int i=0;i<imageId.length;i++){
         RoomImage roomImage=roomImageService.findRoomImageByRoomId(imageId[i]);
         File file=new File(path+roomImage.getImageName());
         if (file.exists()) file.delete();
         roomImageService.deleteRoomImage(imageId[i]);
        }

        return true;
    }
    @RequestMapping("/addImage")
    public String addRoomImage(@RequestParam("file")MultipartFile []file,@RequestParam("roomId")int roomId) throws FileNotFoundException {
        String path = ResourceUtils.getURL("src/main/resources/static/img/").getPath();
        path = path.substring(1);
        System.out.println(path);
        int len = file.length;
        for (int i = 0; i < len; i++) {
            String fileName = file[i].getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffixName;
            File dest = new File(path + fileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file[i].transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            RoomImage roomImage=new RoomImage();
            roomImage.setImageName(fileName);
            roomImage.setRoomId(roomId);
            roomImageService.saveRoomImage(roomImage);
        }
        return "redirect:/room/edit?id="+roomId;
    }
    @GetMapping("/edit")
    public  String updateRoom(@RequestParam("id")int id,Model model){
        Room room=roomService.findRoomByPrimaryKey(id);
        List<RoomImage>roomImages=roomImageService.listRoomImage(id);
        model.addAttribute("roomImages",roomImages);
        model.addAttribute("room",room);
        return "roomUpdate";
    }

    @PostMapping("/edit")
    public  String saveUpdateRoom(Room room){
        int updateRoom = roomService.updateRoom(room);
        int hotelId=room.getHotelId();
        if(updateRoom>0) {
            return "redirect:/room/list?id="+hotelId;
        } else {
            return "redirect:error";
        }
    }

    //黄静
    @RequestMapping("/listByHotelId")
    @ResponseBody
    public List<Room> listByHotelIdAjax(int id){
        return roomService.listRoom(id);
    }

}
